import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { WalletFlow } from './walletFlow.entity';
import { walletFlowEnum } from './walletFlow.enum'

@Injectable()
export class WalletFlowService {
  constructor(
    @InjectRepository(WalletFlow)
    private repository: Repository<WalletFlow>,
  ) {}


  /**
   * 查询用户的所有提现记录
   * @param skip
   * @param take
   * @param wx_user
   * @returns
   */
  async findAll_By_User(skip: number, take: number, wx_user: string): Promise<{data:[],totalPrice: Number, total: Number}> {
    let dataList = await this.repository.query(`
      select substring(a.create_time, 1, 10) create_time,sum(a.use_amount) totalPrice from wallet_flow a
      where a.wx_user = ? and a.status = ?
      group by substring(a.create_time, 1, 10)
      order by substring(a.create_time, 1, 10) desc
      limit ?,?
    `,[wx_user, walletFlowEnum.success, skip, take]);

    let total = await this.repository.query(`
      select count(1) as total from (
        select substring(a.create_time, 1, 10) create_time from wallet_flow a
        where a.wx_user = ? and a.status = ?
        group by substring(a.create_time, 1, 10)
      ) a
    `,[wx_user, walletFlowEnum.success]);

    let totalPrice = await this.repository.query(`
      select sum(a.use_amount) totalPrice from wallet_flow a
      where a.wx_user = ? and a.status = ?
    `,[wx_user, walletFlowEnum.success]);
    return {data:dataList,totalPrice: Number(totalPrice[0].totalPrice), total: Number(total[0].total)}
  }

  /**
   * 查询视频的提现记录
   * @param skip
   * @param take
   * @param video_id
   * @returns
   */
  async findAll_By_Video(skip: number, take: number, video_id: number, wx_user: string): Promise<{data:[],totalPrice:0}> {
    let dataList = await this.repository.query(`
      select substring(a.create_time, 1, 10) create_time,sum(a.unit_price * a.use_duration) totalPrice from profit a
      inner join answer b on b.id = a.answer_id
      where b.video_id = ? and a.status = 1 and a.author_wx_user = ?
      group by substring(a.create_time, 1, 10)
      order by substring(a.create_time, 1, 10) desc
      limit ?,?
    `,[video_id, wx_user, skip, take]);

    let totalPrice = await this.repository.query(`
      select sum(a.unit_price * a.use_duration) totalPrice from profit a
      inner join answer b on b.id = a.answer_id
      where b.video_id = ? and a.status = 1 and a.author_wx_user = ?
    `,[video_id, wx_user]);
    return {data:dataList,totalPrice: totalPrice[0].totalPrice}
  }

  /**
   * 查询提现成功的记录
   * @param wx_user
   * @returns
   */
   findAll_Cash_RecordList(skip: number, take: number, wx_user: string): Promise<WalletFlow[]> {
    return this.repository.find({
      where:{wx_user,status:walletFlowEnum.success},
      skip:skip,
      take:take
    });
  }

  findByWxUser(wx_user: string): Promise<WalletFlow> {
    return this.repository.findOne({where:{wx_user}});
  }

  findWithdrawing(wx_user: string): Promise<WalletFlow> {
    return this.repository.findOne({where:{wx_user,status:walletFlowEnum.withdrawing}});
  }

  findAll(): Promise<WalletFlow[]> {
    return this.repository.find();
  }

  findOne(id: number): Promise<WalletFlow> {
    return this.repository.findOne({where:{id}});
  }

  async create(data: WalletFlow): Promise<WalletFlow> {
    return await this.repository.save(data);
  }

  async update(data: WalletFlow): Promise<WalletFlow> {
    return await this.repository.save(data);
  }
}
